package fun.abbas.demo240621.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class SportView extends View {

    private int paddingAll = 20;
    private int viewSize = 0;
    private final Point viewCenter = new Point();
    private final Paint redPaint = new Paint();
    private final Paint greenPaint = new Paint();
    private final Paint purplePaint = new Paint();
    private final List<RectF> arcBounds = new ArrayList<>(3);
    private final List<Paint> arcPaint = new ArrayList<>();
    private float paintStokeWidth = 88f;
    private int circleNum = 3;

    private final int maxSweepAngle = 270;
    private final int sweepAngleInterval = 18;

    public SportView(Context context) {
        super(context);
        init();
    }

    public SportView(Context context, @Nullable AttributeSet attrs) {

        super(context, attrs);
        init();
    }

    public SportView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public SportView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        redPaint.setColor(Color.RED);
        redPaint.setStyle(Paint.Style.STROKE);
        redPaint.setStrokeWidth(paintStokeWidth);

        greenPaint.setColor(Color.RED);
        greenPaint.setStyle(Paint.Style.STROKE);
        greenPaint.setStrokeWidth(paintStokeWidth);

        purplePaint.setColor(Color.RED);
        purplePaint.setStyle(Paint.Style.STROKE);
        purplePaint.setStrokeWidth(paintStokeWidth);

        arcPaint.addAll(Arrays.asList(
                redPaint, greenPaint, purplePaint
        ));
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.EXACTLY) {
            int wSize = MeasureSpec.getSize(widthMeasureSpec);
            initParams(wSize);
            return;
        }
        if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.EXACTLY) {
            int hSize = MeasureSpec.getSize(heightMeasureSpec);
            initParams(hSize);
            return;
        }
        initParams(200);
    }

    private void initParams(int size) {
        viewSize = size;
        paddingAll = viewSize / 10;
        int center = (viewSize - paddingAll) / 2;
        viewCenter.set(center, center);
        for (int i = 0; i < circleNum; i++) {
            arcBounds.add(
                    new RectF(
                            paddingAll * (i +1),
                            paddingAll * (i + 1),
                            viewSize - paddingAll - paintStokeWidth / 2 * i + 1,
                            viewSize - paddingAll - paintStokeWidth / 2 * i + 1
                    )
            );
        }
        setMeasuredDimension(size, size);
    }

    @Override
    protected void onDraw(@NonNull Canvas canvas) {
        for (int i = 0; i < circleNum; i++) {
            int angle = maxSweepAngle - sweepAngleInterval * i;
            canvas.drawArc(arcBounds.get(i),
                    getStartAnge(angle),
                    angle,
                    false,
                    arcPaint.get(i)
            );
        }
    }

    private final int maxAngle = 360;

    private int getStartAnge(@IntRange(from = 0, to = 360) int angle) {
        return (angle / 2);
    }
}
